<template>
  <div id="app">
    <!-- 导航菜单 -->
    <nav class="nav-bar" v-if="$route.path !== '/login' && $route.path !== '/register' && isLoggedIn">
      <div class="nav-container">
        <div class="nav-left">
          <router-link to="/service-appointment-and-matching" class="nav-link">服务预约与匹配</router-link>
          <router-link to="/health-management" class="nav-link">健康管理</router-link>
          <router-link to="/social-activities" class="nav-link">社交与活动</router-link>
          <router-link to="/time-bank" class="nav-link">时间银行</router-link>
        </div>
        <div class="nav-right">
          <span class="welcome-text">欢迎, {{ username }}</span>
          <router-link to="/my-services" class="nav-link">我的服务</router-link>
        </div>
        <div class="nav-logout">
          <button @click="handleLogout" class="logout-btn">退出登录</button>
        </div>
      </div>
    </nav>
    <div class="main-content" :class="{ 'with-nav': isShowNav }">
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      username: '',
      isUserLoggedIn: false
    }
  },
  computed: {
    isLoggedIn() {
      return this.isUserLoggedIn;
    },
    isShowNav() {
      return this.isLoggedIn && this.$route.path !== '/login' && this.$route.path !== '/register';
    }
  },
  methods: {
    handleLogout() {
      localStorage.removeItem('user');
      this.isUserLoggedIn = false;
      this.username = '';
      this.$router.push('/login');
    },
    updateUsername() {
      const user = localStorage.getItem('user');
      if (user) {
        this.username = JSON.parse(user).username;
        this.isUserLoggedIn = true;
      } else {
        this.isUserLoggedIn = false;
        this.username = '';
      }
    }
  },
  mounted() {
    this.updateUsername();
    // 如果用户未登录且不在登录/注册页面，重定向到登录页面
    if (!this.isLoggedIn && !['/login', '/register'].includes(this.$route.path)) {
      this.$router.push('/login');
    }
  },
  watch: {
    $route() {
      this.updateUsername();
    }
  },
  created() {
    this.updateUsername();
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  min-height: 100vh;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}

.main-content {
  flex: 1;
  min-height: calc(100vh - 60px);
  transition: padding-top 0.3s ease;
}

.main-content.with-nav {
  padding-top: 60px;
}

.nav-bar {
  background-color: #42b983;
  padding: 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  height: 60px;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 100%;
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 20px;
}

.nav-left {
  display: flex;
  gap: 20px;
  align-items: center;
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.nav-logout {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.nav-link {
  text-decoration: none;
  color: white;
  font-weight: 500;
  font-size: 16px;
  transition: all 0.3s ease;
  padding: 8px 16px;
  border-radius: 4px;
}

.nav-link:hover,
.nav-link.router-link-active {
  color: #ffffff;
  background-color: rgba(255, 255, 255, 0.2);
}

.welcome-text {
  color: white;
  font-size: 14px;
}

.logout-btn {
  background: transparent;
  border: 1px solid white;
  color: white;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* 重置全局样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  background-color: #f5f5f5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-container {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    padding: 10px;
    gap: 10px;
  }
  
  .nav-bar {
    height: auto;
  }
  
  .nav-left {
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .nav-right {
    justify-content: center;
  }
  
  .nav-logout {
    justify-content: center;
  }
  
  .main-content.with-nav {
    padding-top: 120px;
  }
}
</style>